package com.ellricky.market.ui

import android.content.Intent
import android.os.Bundle
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.bnb.BnbUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.course.JobTrainFragment
import com.ellricky.baselib.adapter.course.JobTrainingChildFragment
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.http.callback.ECallback
import com.ellricky.baselib.ui.ActiveDocFragment
import com.ellricky.baselib.ui.SchoolOverviewFragment
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.market.MarketUserEntity
import com.ellricky.market.R
import kotlinx.android.synthetic.main.market_activity_main.*

class MarketMainActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private lateinit var userEntity: MarketUserEntity

    override fun getContentLayout(): Int {
        return R.layout.market_activity_main
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LocalCacheUtil.setIsReloadData(true)
    }


    override fun onPause() {
        super.onPause()
        LocalCacheUtil.setIsReloadData(false)
    }

    override fun initUIView() {
        val jobBundle = Bundle().apply {
            putString(BaseConstant.KEY_TYPE, BaseConstant.TYPE_JOB_TRAIN)
            putBoolean(BaseConstant.KEY_SHOW_TITLE, true)
        }
        mainView.initVP(
            listOf(
                MarketClueFragment.newInstance(),
                MarketActiveManageFragment.newInstance(),
                JobTrainFragment(),
                ActiveDocFragment.newInstance(),
                SchoolOverviewFragment.newInstance(),
            ), supportFragmentManager
        )

        mainView.initBnb(
            mutableListOf(
                BnbUtil.addBnbItem(
                    this,
                    R.drawable.base_icon_achivement_sel,
                    R.drawable.base_icon_achivement_nor,
                    R.string.base_clue_manage
                ),
                BnbUtil.addBnbItem(
                    this,
                    R.drawable.base_icon_rank_sel,
                    R.drawable.base_icon_rank_nor,
                    R.string.base_active_manage
                ),

                BnbUtil.addBnbItem(
                    this,
                    R.drawable.base_icon_integral_sel,
                    R.drawable.base_icon_integral_nor,
                    R.string.base_job_training_title
                ),
                BnbUtil.addBnbItem(
                    this,
                    R.drawable.pd_icon_am_sel,
                    R.drawable.pd_icon_am_nor,
                    R.string.base_class_active
                ),
                BnbUtil.addBnbItem(
                    this,
                    R.drawable.base_icon_integral_sel,
                    R.drawable.base_icon_integral_nor,
                    R.string.base_my_achievement
                )
            )
        )
    }

    private fun loadData() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf<String, Any>()
        HttpUtils.getInstance().post(url, paramMap, object : ECallback<MarketUserEntity>(this) {
            override fun onSuccess(data: MarketUserEntity, msg: String) {
                userEntity = data
            }
        })
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode > 0) loadData()
    }


    override fun onKeyBack(isKeyBack: Boolean) {
        if (isKeyBack) EAppUtil.exitApp(this)
    }
}
